package com.yanqu.road.dao.impl.scenecard;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suitang.data.SuiTangUserData;
import com.yanqu.road.entity.scenecard.SceneCardPlayerData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SceneCardDaoImpl extends TempDataObjectDao<SceneCardPlayerData> {

    public static String getSceneCardPlayerDataSql = "select * from `t_u_scene_card`";
    public Map<Integer, SceneCardPlayerData> getSceneCardPlayerData() {
        Map<Integer, SceneCardPlayerData> ret = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSceneCardPlayerDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                SceneCardPlayerData data = parse(rs);
                ret.put(data.getCardId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return ret;
    }

    public static String updateSceneCardPlayerDataSql = "update `t_u_scene_card` set touchTime=? where cardId=?";
    @Override
    protected boolean updateData(SceneCardPlayerData sceneCardPlayerData) {
        sceneCardPlayerData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, sceneCardPlayerData.getTouchTime()));
        para.put(i++, new DbParameter(Types.INTEGER, sceneCardPlayerData.getCardId()));
        if (getDb().executeNoneQuery(updateSceneCardPlayerDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSceneCardPlayerDataSql = "insert into `t_u_scene_card`(cardId,touchTime) values (?,?)";
    @Override
    protected boolean addData(SceneCardPlayerData sceneCardPlayerData) {
        sceneCardPlayerData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sceneCardPlayerData.getCardId()));
        para.put(i++, new DbParameter(Types.BIGINT, sceneCardPlayerData.getTouchTime()));
        if (getDb().executeNoneQuery(insertSceneCardPlayerDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SceneCardPlayerData parseData(ResultSet rs) throws SQLException {
        SceneCardPlayerData sceneCardPlayerData = new SceneCardPlayerData();
        sceneCardPlayerData.setCardId(rs.getInt("cardId"));
        sceneCardPlayerData.setTouchTime(rs.getLong("touchTime"));

        return sceneCardPlayerData;
    }

}
